package com.sirap.spring.boot.demo.helper;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sirap.basic.component.Konstants;
import com.sirap.basic.thirdparty.ServletHelper;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.EmptyUtil;
import com.sirap.basic.util.NetworkUtil;
import com.sirap.basic.util.RandomUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.common.command.CommandBase;
import com.sirap.common.command.explicit.CommandSirap;
import com.sirap.common.framework.Janitor;
import com.sirap.common.framework.Stash;
import com.sirap.spring.boot.demo.config.FishKonfig;

public class SirapHelper {

	public static final String KEY_DEMO = "demo";
	public static final String KEY_MY_IP = "myip";
	public static final String[] SIMPLE_MATH = {"simple math calculation", "1+2.3x(4.5-_6)/8"};
	
	public static List handleByCommands(String input) {
		boolean hasBeenHandled = Janitor.g().process(input);
		List result = Lists.newArrayList();
		if(hasBeenHandled) {
			result.addAll(Stash.g().getLastQuery().getResult());
		}
		
		return result;
	}

	public static List processBeforeCommands(String input, HttpServletRequest request) {

		if (StrUtil.equals(input, KEY_MY_IP)) {
			List<String> items = Lists.newArrayList();
			String ip = ServletHelper.getOriginIp(request);
			items.add("Your IP:   " + ip);
			if(NetworkUtil.isLegalIP(ip)) {
				List<String> detail = NetworkUtil.ipDetail(ip, false);
				items.addAll(detail);
			}
			Stash.g().saveLastQuery(input, "", items);
			
			return items;
		}

		if (StrUtil.equals(input, CommandSirap.KEY_LASTLIST + CommandBase.KEY_2DOTS)) {
			List list = Stash.g().getQueryNames();
			return list;
		}

		String solo = StrUtil.parseParam(CommandSirap.KEY_LASTLIST + "(|\\d{1,3})", input);
		if (solo != null) {
			int k = solo.isEmpty() ? 1 : Integer.parseInt(solo);
			List list = Stash.g().getLastKQuery(k).getResult();
			return list;
		}

		if (StrUtil.equals(KEY_DEMO + CommandBase.KEY_2DOTS, input)) {
			List<String> items = FishKonfig.aka10Props().getValuesBySomeKeyword("demo.exp");
			Stash.g().saveLastQuery(input, "", items);
			
			return items;
		}

		return null;
	}
	
	public static String[] randomDemo() {
		List<String[]> items = demos();
		int random = RandomUtil.number(items.size());
		String[] demo = items.get(random);
		return demo;
	}
	
	public static List<String[]> demos() {
		List<String> items = FishKonfig.aka10Props().getValuesBySomeKeyword("demo.exp");
		String regex = "(.+?)#(.+)";
		List<String[]> demos = Lists.newArrayList();
		for(String item : items) {
			String[] params = StrUtil.parseParams(regex, item);
			if(params != null) {
				demos.add(params);
			}
		}
		
		if(demos.isEmpty()) {
			demos.add(SIMPLE_MATH);
		}
		
		return demos;
	}
	
	public static Map<String, Object> calculate(HttpServletRequest request) {
		String keyAjax = "ajax";
		String exp = request.getParameter("exp");
		boolean isAjax = request.getParameter(keyAjax) != null;
		D.pl(request.getParameter(keyAjax));
//		D.pjsp(ServletHelper.detailX(request));
//		D.pl(useAjax);
		Map<String, Object> map = Maps.newConcurrentMap();
		map.put("result", Konstants.FAKED_EMPTY);
		if(isAjax) {
			map.put(keyAjax, true);
		}
		map.put("random", StrUtil.truncate(RandomUtil.name(), 6));
		map.put("buttonWidth", FishKonfig.aka10Props().getX("cal.button.width", "48"));
		
		D.ts("isAjax: " + isAjax);
		D.ts("exp: " + exp);
		
		if(EmptyUtil.isNullOrEmptyOrBlank(exp)) {
			String[] demo = SirapHelper.randomDemo();
			map.put("exp", demo[1]);
			map.put("result", StrUtil.occupy("[{0}]", demo[0]));
			return map;
		}
		
		String command = exp.trim();
		String info = "";
		if(StrUtil.equals(SirapHelper.KEY_DEMO, command)) {
			String[] demo = SirapHelper.randomDemo();
			info = StrUtil.occupy("[{0}] ", demo[0]);
			command = demo[1];
		}
		
		List templist = SirapHelper.processBeforeCommands(command, request);
		if(templist == null) {
			templist = SirapHelper.handleByCommands(command);
		} else {
			templist.add(0, "$ " + command);
		}
		if(EmptyUtil.isNullOrEmpty(templist)) {
			templist = Lists.newArrayList(Konstants.FAKED_EMPTY);
		}
		D.list(templist);
		String strResult = info + StrUtil.connectWithLineSeparator(templist); 
		map.put("exp", command);
		map.put("result", strResult);
//		D.pjsp(map);
		
		return map;
	}

	public static final String USERNAME_GUEST = "guest";
}
